<template>
	<view class="calculat-container" v-if="total !== 0">
		<!-- 全选 -->
		<label class="radio">
			<radio color="#c00000" :checked="isFullCheck"/><text>全选</text>
		</label>
		<!-- 合计 -->
		
		<view class="amount-box">
			合计: <text class="amount">￥{{checkGoodsAmount}}</text>
		</view>
		<!-- 结算 -->
		<view class="btn-settle" @click="settlement">结算({{checkCount}})</view>
	</view>
	<view v-else class="cart-empty-image">
		<image src="https://s3.bmp.ovh/imgs/2022/03/9a2059728a56db5e.png"></image>
	</view>
</template>

<script>
	import { mapGetters, mapState } from 'vuex'
	export default {
		name:"my-settle",
		computed: {
			...mapState('m_cart', ['cart']),
			...mapGetters('m_cart', ['checkCount', 'total', 'checkGoodsAmount']),
			...mapState('m_user', ['token']),
			isFullCheck() {
				return this.total === this.checkCount
			}
		},
		data() {
			return {
				seconds: 3,
				timer: null
			};
		},
		methods: {
			settlement() {
				// console.log(this.checkCount)
				//判断选择购买商品的数量，如果===0 说明未选择，不进行购买
				if (this.checkCount === 0) uni.$showMessage("请先选择您想要购买的商品！")
				//判断用户是否登录，未登录，则显示提示信息，进行登录
				if (!this.token) return this.delayNavigate()
				
				this.payOrder()
			},
			payOrder() {
				// 1.创建订单
				// 1.1组织订单的对象
				const orderInfo = {
					//这里是订单的真正的价格，但是因为开发，所以这里暂时使用的是1分钱进行演示
					// order_price: this.checkGoodsAmount,
					order_price: 0.01,
					//记录所有勾选状态的商品
					goods: this.cart.filter(x => x.goods_state)
				}
				
				//1.2进行订单的预支付
				//3.微信支付
				//进行模拟支付
				uni.$showMessage("支付完成！")
			},
			
			showTips() {
				uni.showToast({
					icon: 'none',
					title: '请先登录,再进行结算，稍后将自动跳转到登录页面！',
					mask: true,
					duration: 1500
				})
			},
			delayNavigate() {
				this.showTips()

				this.timer = setInterval(() => {
					this.seconds--
					
					if (this.seconds <= 0) {
						clearInterval(this.timer)
						
						uni.switchTab({
							url:'/pages/My/My'
						})
						this.seconds = 3
						return 
					}
					
					this.showTips()
				}, 1000)
			}
		}
	}
</script>

<style lang="scss">
.calculat-container {
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
	height: 50px;
	
	display: flex;
	justify-content: space-between;
	align-items: center;
	background-color: white;
	font-size: 14px;
	padding-left: 5px;
	.radio {
		display: flex;
		align-items: center;
	}
	
	.amount-box {
		.amount {
			color: #c00000;
			font-weight: bold;
		}
	}
	
	.btn-settle {
		background-color: #c00000;
		color: white;
		height: 50px;
		line-height: 50px;
		padding: 0 10px;
		min-width: 90px;
		text-align: center;
	}
}

.cart-empty-image {
	background-color: #FFFFFD;
	padding-top: 80px;
}
</style>
